//发货单审核（零担）



/**
 * 主视图
 */
interface IOrderAuditBreakBulkDetailViewModel extends ng.IScope {
    /**
     * 订单列表数据对象(总单)
     */
    orderList: Array<invoiceManagementOrder>;
    /**
     * 承运单数据对象
     */
    CData: invoiceManagement;
    /**
     * 发货单数据对象
     */
    OData: invoiceManagementOrder;
    /**
     * 子单列表数据对象
     */
    orderChildrenList: Array<invoiceManagementOrderChild>;
    /**
     * 子单详情对象
     */
    orderChil: any;
    /**
     * 车辆定位数据视图
    */
    carLocation: CarLoactionData;
    /**
     * 退回派车按钮显示隐藏
     */
    backShow: boolean;
    /**
     * 退回派车按钮启用禁用
     */
    backDis: boolean;
    /**
     * 确定按钮显示隐藏
     */
    saveBtn: boolean;
    /**
     * 确定按钮启用禁用
     */
    saveDis: boolean;
    /**
     * 备用文本启用禁用
     */
    remarksDis: boolean;
    /**
    * 附件信息显示隐藏
    */
    hideAttachment: boolean;
    /**
     * 点击获取订单详情事件
     */
    orderBreakList: (data: invoiceManagementOrder) => void;
    /**
     * 点击获取子单详情事件
     */
    EditChildren: (data: invoiceManagementOrderChild) => void;
    /**
     * 驾驶证上传组件
     */
    driverUploader: any;
    /**
     * 退回派车事件
     */
    returnOrderC: () => void;
    /**
     * 确定事件
     */
    confirmC: () => void;
    /**
     * 取消事件
     */
    cancelC: () => void;
    /**
     * APP竞价结果
     */
    orderAppBiddingList: Array<quotationResponse>;
    /**
     * 定位按钮点击事件
     */
    location: (carid: string, driverPhone: string) => void;
    /////////////////////////////////////////////////
    //定位视图
    carId: string;
    /**
    *车牌号
    */
    carCode: string;
    /**
     * 是否为车辆定位
     */
    isVehicle:boolean;
    /**
    *送货地址
    */
    deliverAddress: string;
    deliverAddress_?:string
    /**
    *手机号
    */
    phoneNumber: string;
    /**
    *发货时间
    */
    shipAddress: string;
    shipAddress_:string;
    /**
    *历史定位地点
    */
    areaS: string;
    /**
    *历史定位时间
    */
    creationTimeS: string;
    /**
    *定位注册按钮启用禁用
    */
    isdisabled1: boolean;
    /**
    *定位按钮启用禁用
    */
    isdisabled2: boolean;
}

interface CarLoactionData{
    /**
     * 车辆ID
     */
     carId: string;
    /**
    *车牌号
    */
    carCode: string;
    /**
    *送货地址
    */
    deliverAddress: string;
    deliverAddress_?:string
    /**
    *手机号
    */
    phoneNumber: string;
    /**
    *发货时间
    */
    shipAddress: string;
    shipAddress_:string;
    /**
    *历史定位地点
    */
    areaS: string;
    /**
    *历史定位时间
    */
    creationTimeS: string;
    /**
    *定位注册按钮启用禁用
    */
    isdisabled1: boolean;
    /**
    *定位按钮启用禁用
    */
    isdisabled2: boolean;
}

class OrderAuditBreakBulkDetailController {
    uploaderList: AddCarAttachmentDetail[];
    baseImageUrl: string;
    /**
     * 承运单id
     */
    guId: string;
    /**
     * 零担页面
     */
    name: string;
    /**
     * 订单状态
     */
    type: string;
    /**
     * 承运单商id
     */
    carrierId: string;

    constructor(private $scope: IOrderAuditBreakBulkDetailViewModel,
        private routeService: routeService,
        
        private locationService: ILocationService,
        private valueService: IValueService,
        private orderService: IorderService,
        private employeeService: IEmployeeService,
        private $state: angular.ui.IStateService,
        //private areaApiService: IAreaService,
        private $location: ng.ILocationService,
        private $http: ng.IHttpService,
        private $ngBootbox: BootboxService,
        private commonService:ICommonService,
        private ossUploaderService:ossUploaderService) {
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";
        this.uploaderList = [];
        this.init();
    }

    init = () => {
        this.guId = this.$location.search().id;
        this.name = this.$location.search().name;
        this.type = this.$location.search().type;

        this.$scope.hideAttachment = true;
        this.$scope.backShow = true; 
        this.$scope.saveBtn = true;

        this.$scope.returnOrderC = this.returnOrderC;
        this.$scope.confirmC = this.confirmC;
        this.$scope.cancelC = this.cancelC;
        this.$scope.location = this.location;
        this.$scope.orderBreakList = this.orderBreakList;
        this.$scope.EditChildren = this.EditChildren;

      

        const OssFile = this.ossUploaderService.getOssFile();
        this.$scope.driverUploader= new OssFile(this.$scope,this.$scope.driverUploader,{uploaderList:this.uploaderList,tag:1,multiple:true});
       

        if (this.type != "已派车") { this.$scope.backShow = false; this.$scope.saveBtn = false; this.$scope.remarksDis = true; };
        this.loaData();
    }

    //加载载所有数据
    loaData = () => {
        this.orderService.invoiceManagementDetail(this.guId).then((res) => {
            this.$scope.orderList = res.data.orderList;
            this.$scope.CData = res.data;
        })
    }

    /**
    * 获取订单详情事件
    */
    orderBreakList = (data: invoiceManagementOrder) => {
        this.$scope.OData = data;
        this.$scope.orderChildrenList = data.childList;

        this.orderService.quotation(this.$scope.OData.id).then((res) => {
            this.$scope.orderAppBiddingList = res.data.data;
        })

        this.$scope.driverUploader.clearQueue();
        var attachments = this.$scope.OData.orderAttachments;
        if (attachments.length > 0) {
            attachments.forEach((item, index) => {
                // this.commonService.showFileItem(item, this.$scope.driverUploader);
                this.$scope.driverUploader.addByAttach(item)
                this.uploaderList.push(item);
            })
        }
    }

    /**
     * 退回派车
     */
    returnOrderC = () => {
        this.$scope.backDis = true;
        this.$ngBootbox.confirm('确定退回派车吗?').then(() => {
            this.orderService.editOrderStatusByCarrierOrderId(this.guId, this.$scope.CData.remarks, "4", "", "").then((res) => {
                this.$scope.backDis = false;
                this.$state.go("app.order.orderAuditController");//跳转发货单受理
            }, (rej) => { this.$scope.backDis = false; });
        }, () => { this.$scope.backDis = false; return; });
    }

    /**
     * 确定事件
     */
    confirmC = () => {
        this.$scope.saveDis = true;
        this.$ngBootbox.confirm('确定审核通过吗?').then(() => {
            this.orderService.editOrderStatusByCarrierOrderId(this.guId, this.$scope.CData.remarks, "5", "", "").then((res) => {
                this.$scope.saveDis = false;
                this.$state.go("app.order.orderAuditController");//跳转发货单受理
            }, (rej) => { this.$scope.saveDis = false; });
        }, () => { this.$scope.saveDis = false; return });
    }

    /**
     * 取消事件
     */
    cancelC = () => {
        this.$state.go("app.order.orderAuditController", { name: 'audit' });//跳转发货单受理
    }

    /**
    * 点击获取子单详情事件
    */
    EditChildren = (data: invoiceManagementOrderChild) => {
        this.$scope.orderChil = data;
    }

    /**
     * 过滤图片格式
     */
    // ownFilter(file: File): boolean {
    //     let self:any = this;
    //     return self.queue.length < 10;
    // }

    /**
     * 选择上传文件后
     */
    onSuccessItem = (fileName: string, filePath: string, tag: number) => {
        let attachment = {
            attachmentId: "",
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: tag.toString()
        };
        this.uploaderList.push(attachment);
    }

    /**
     * 删除上传的附件
     */
    onDelete = (item: any) => {
        $("input[type=file]").val("");
        var imgName = item._file.name;
        this.uploaderList.forEach((item, index) => {
            if (item.attachmentName == imgName) {
                this.uploaderList.splice(index, 1);
            }
        });
    }

 


    /**
    * 定位按钮点击事件
    */
    location = (carid: string, driverPhone: string) => {
        var carid = carid;
        var driverPhone = driverPhone;
         this.$scope.carId=carid;
        this.$scope.isVehicle=true;
        
        //定位字段初始化
        this.$scope.carCode = this.$scope.CData.carCode;

        this.$scope.deliverAddress_ = "";
        this.$scope.phoneNumber = this.$scope.CData.driverPhone;
        this.$scope.shipAddress_ = "";
        this.locationService.getHistoryLocation(carid, true).then((resq) => {
            this.$scope.areaS = resq.data.area;
            this.$scope.creationTimeS = resq.data.creationTime;
        }, (rel) => { console.log(rel); });

        this.locationService.getLocationState(carid, true).then((res) => {
            if (res.data.isRegister == true) {
                this.$scope.isdisabled1 = true;
                this.$scope.isdisabled2 = false;
            } else {
                this.$scope.isdisabled1 = false;
                this.$scope.isdisabled2 = true;
            }
        }, (rej) => { this.$scope.isdisabled1 = true; this.$scope.isdisabled2 = true; })
    }
}

angular.module("tms").controller("orderAuditBreakBulkDetailController", ["$scope",
    "routeService", 
    "locationService", "valueService",
    "orderService", "employeeService",
    "$state",
    //"areaApiService",
     "$location",
    "$http", "$ngBootbox","commonService","ossUploaderService", OrderAuditBreakBulkDetailController]);